var core = require('core');
var wechat = core.wechat;
var sign = require('./sign');
var util = core.util;
var qr = util.qrcode;
var addSupervisor = function(req, res, next){

    var body = req.body;
    var token = body.token;
    var mid = body.mid;

    sign(token, function(doc){
        if(doc.code)
            return res.send(doc);

        var param = {
            openid : doc.docs.data.oid
        };

        core.api("open/setToken", param, function(err, body){
            if(!doc.code){
                var url = wechat.config.URL + "open/supervisor_create?token="+ body.docs.token +"&mid="+mid+"&oid="+doc.docs.data.oid;
                var options = {
                    type: "png"
                };
                var img = qr(url ,options);
                doc.docs = {
                    qrcode : img
                };
            }
            res.send(doc);
        });
    });

};

module.exports = addSupervisor;
